package com.tlgen.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import com.tlgen.R;
import com.tlgen.activity.LoginActivity;
import com.tlgen.activity.PayGuideActivity;
import com.tlgen.activity.PayOrderActivity;
import com.tlgen.adapter.BannerAdapter;
import com.tlgen.adapter.CommonGridAdapter;
import com.tlgen.data.DemoDataProvider;
import com.xuexiang.rxutil2.rxjava.RxJavaUtils;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.banner.recycler.BannerLayout;
import com.xuexiang.xui.widget.textview.MarqueeTextView;
import com.xuexiang.xui.widget.textview.marqueen.DisplayEntity;
import java.util.Arrays;
import java.util.List;

/**
 * VLayout使用步骤
 * 1.设置VirtualLayoutManager
 * 2.设置RecycledViews复用池大小（可选）
 * 3.设置DelegateAdapter
 */
public class HomeFragment extends Fragment {

    //传递过来的参数Bundle，供子类使用
    protected Bundle args;

    /**
     * 创建fragment的静态方法，方便传递参数
     * @param args 传递的参数
     * @return
     */
    public static <T extends Fragment>T getInstance(Class clazz, Bundle args) {
        T mFragment=null;
        try {
            mFragment= (T) clazz.newInstance();
        } catch (java.lang.InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        mFragment.setArguments(args);
        return mFragment;
    }

    /**
     * 初始创建Fragment对象时调用
     * @param savedInstanceState
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        args = getArguments();
    }

    TitleBar mTitleBar;

    // 文字滚动
    MarqueeTextView mTvMarquee;

    private View view;

    // 轮播
    private BannerAdapter mAdapter;
    private BannerLayout mBl;

    public String[] urls = new String[]{//轮播图片地址
            "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3644598630,1332086117&fm=26&gp=0.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1608018158387&di=9bd196ec924b9e18fc6ec119fafdc170&imgtype=0&src=http%3A%2F%2Fsc.sinaimg.cn%2F2014%2F0514%2FU8161P841DT20140514151630.jpg",
            "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=264264963,3796148581&fm=26&gp=0.jpg",
            "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=209776737,557850264&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=143700099,2006340227&fm=26&gp=0.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1608017961612&di=69a1ca1fa5c0b41b5d54e044d02bbf37&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn02%2F133%2Fw600h333%2F20180524%2Fa00b-haysvix5319181.jpg",
    };

    RecyclerView recyclerHead;

    private CommonGridAdapter mGridAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, container, false);

        mBl = view.findViewById(R.id.bl);
        mAdapter = new BannerAdapter(getContext(),urls);
        mBl.setAdapter(mAdapter);

        mTitleBar = view.findViewById(R.id.titlebar);
        mTitleBar.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), LoginActivity.class));
            }
        });


        // 九宫格
        recyclerHead = view.findViewById(R.id.recycler_head);
        WidgetUtils.initGridRecyclerView(recyclerHead, 4, 0);
        recyclerHead.setAdapter(mGridAdapter = new CommonGridAdapter(true));
        mGridAdapter.refresh(DemoDataProvider.getGridItems(getContext()));


        // 文字滚动
        final List<String> datas = Arrays.asList("您的贷款、还款记录已报送人民银行个人征信系统,", "请大家务必按时还款。", "如有违约，会影响您的诚信记录，", "今后在办理信用卡、房贷、车贷等各类贷款时也会有不良影响。", "高校助学贷款系统 V7.5.1。");

        mTvMarquee = view.findViewById(R.id.tv_marquee);
        mTvMarquee.setOnMarqueeListener(new MarqueeTextView.OnMarqueeListener() {
            @Override
            public DisplayEntity onStartMarquee(DisplayEntity displayMsg, int index) {
                if ("离离原上草，一岁一枯荣。".equals(displayMsg.toString())) {
                    return null;
                } else {
                    return displayMsg;
                }
            }

            @Override
            public List<DisplayEntity> onMarqueeFinished(List<DisplayEntity> displayDatas) {
                return displayDatas;
            }
        });
        mTvMarquee.startSimpleRoll(datas);

        RxJavaUtils.delay(5, aLong -> mTvMarquee.removeDisplayString(datas.get(3)));

        view.findViewById(R.id.to_pay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().startActivity(new Intent(getContext(), PayOrderActivity.class));
            }
        });
        view.findViewById(R.id.to_pay_guide).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().startActivity(new Intent(getContext(), PayGuideActivity.class));
            }
        });

        return view;
    }


}
